/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kursova_perzeptron;

import java.util.Arrays;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.learning.DataSet;
import org.neuroph.core.learning.DataSetRow;
import org.neuroph.nnet.MultiLayerPerceptron;

/**
 * @author ruslan
 */
public class Perceptron {
    // перші 8 - колектори, наступні 7 - покришки

    double[][] inputs = {
        {18.9, 10.0, 8.6, 22},
        {12.6, 15.1, 14.4, 10},
        {14.1, 7.8, 12.3, 12},
        {15.0, 9.5, 12.8, 8},
        {15.6, 12.4, 9.0, 17},
        {17.8, 16.7, 7.5, 5},
        {10.7, 22.2, 11.9, 14},
        {11.5, 17.4, 18.2, 7},
        {10.9, 15.6, 17.9, 8},
        {12.6, 40.1, 8.5, 4},
        {9.5, 27.8, 12.4, 5},
        {8.8, 18.9, 25.0, 3},
        {12.0, 33.5, 8.6, 3},
        {9.0, 14.7, 19.7, 7},
        {8.5, 15.0, 22.4, 4}};
    double[][] outputs = {
        {Math.random() * 0.5},
        {Math.random() * 0.5},
        {Math.random() * 0.5},
        {Math.random() * 0.5},
        {Math.random() * 0.5},
        {Math.random() * 0.5},
        {Math.random() * 0.5},
        {Math.random() * 0.5},
        {0.5 + Math.random() * 0.5},
        {0.5 + Math.random() * 0.5},
        {0.5 + Math.random() * 0.5},
        {0.5 + Math.random() * 0.5},
        {0.5 + Math.random() * 0.5},
        {0.5 + Math.random() * 0.5},
        {0.5 + Math.random() * 0.5}};
    int numberOfInputNeurons = inputs[0].length;
    int numberOfOutputNeurons = outputs[0].length;
    int numberOfinputs = inputs.length;
    double[][] weights;

    public Perceptron() {
        weights = new double[numberOfInputNeurons][numberOfOutputNeurons];
        for(int i = 0; i < numberOfInputNeurons; i++)
            for(int j = 0; j < numberOfOutputNeurons; j++){
                weights[i][j] = Math.random();
            }
        normalizeData();
    }

    public void normalizeData() {
        double[] sum = new double[inputs.length];
        for (int i = 0; i < inputs.length; i++) {
            sum[i] = 0;
            for (int j = 0; j < inputs[i].length; j++) {
                sum[i] += Math.pow(inputs[i][j], 2);
            }
            sum[i] = Math.sqrt(sum[i]);
        }
        for (int i = 0; i < inputs[i].length; i++) {
            for (int j = 0; j < inputs[i].length; j++) {
                inputs[i][j] /= sum[i];
            }

        }
    }

    public void weightsSetting() {
        double learningFactor = 0.005;
        int q = 0;
        while (q++ < 100) {
            for (int i = 0; i < numberOfinputs; i++) {
                double output = calculateOutput(i);
                for (int j = 0; j < numberOfOutputNeurons; j++) {
                    if (outputs[i][j] != output) {
                        for (int k = 0; k < numberOfInputNeurons; k++) {
                            weights[k][j] = weights[k][j] + learningFactor
                                    * inputs[i][k]
                                    * (outputs[i][j] - output);
                        }
                    }
                }
            }
        }
    }

    double calculateOutput(int inputsNo) {
        double limit = 0.5;
        double result;
        double[] toImpress = inputs[inputsNo];
        double res = weights[0][0] * toImpress[0] + weights[1][0]
                * toImpress[1] + weights[2][0] * toImpress[2]
                + weights[3][0] * toImpress[3];
        if (res < limit) {
            result = outputs[inputsNo][0];
        } else {
            result = res;
        }
        return result;
    }

    public double usePerceptron(double x1, double x2, double x3, double x4) {
        double d;
        double d1 = x1;
        double d2 = x2;
        double d3 = x3;
        double d4 = x4;
        d = Math.sqrt(d1 * d1 + d2 * d2 + d3 * d3 + d4 * d4);
        double x = weights[0][0] * (d1 / d) + weights[1][0] * (d2 / d)
                + weights[2][0] * (d3 / d) + weights[3][0] * (d4 / d);
        if (x < 0.5) {
            System.out.println("Колектор");
        } else {
            System.out.println("Покришка");
        }
        return d;
    }

    public static void main(String[] args) {
        Perceptron p = new Perceptron();
        p.usePerceptron(0, 0, 0, 0);
        //////////////////////////////////////////////

    }
}
